package com.wind.weblog.common.domain.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.wind.weblog.common.domain.dos.TagDO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

public interface TagMapper extends BaseMapper<TagDO> {
    @Select("SELECT c.id, c.name, c.create_time, c.update_time, c.is_deleted, COUNT(acr.article_id) AS articlesTotal\n " +
            "FROM t_tag c\n " +
            "LEFT JOIN t_article_tag_rel acr ON c.id = acr.tag_id\n " +
            "GROUP BY c.id, c.name, c.create_time, c.update_time, c.is_deleted\n")
    List<TagDO> selectTagAndArticleInfo();

    @Select({
            "<script>",
            "SELECT c.id, c.name, c.create_time, c.update_time, c.is_deleted, COUNT(acr.article_id) AS articlesTotal",
            "FROM t_tag c",
            "LEFT JOIN t_article_tag_rel acr ON c.id = acr.tag_id",
            "<if test='tagIds != null and !tagIds.isEmpty()'>",
            "WHERE c.id IN",
            "<foreach collection='tagIds' item='tagId' open='(' separator=',' close=')'>",
            "#{tagId}",
            "</foreach>",
            "</if>",
            "GROUP BY c.id, c.name, c.create_time, c.update_time, c.is_deleted",
            "</script>"
    })
    List<TagDO> selectTagAndArticleInfoById(@Param("tagIds") List<Long> tagIds);
}
